package com.wsoft.system.controller;

import com.wsoft.core.service.CommonConfigService;
import com.wsoft.response.ResponseResult;
import com.wsoft.system.dto.TsConfigDto;
import com.wsoft.system.query.config.ConfigListQuery;
import com.wsoft.system.query.config.TsConfigReq;
import com.wsoft.system.service.ITsConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 系统配置表 前端控制器
 * </p>
 *
 * @author kyle
 * @since 2024年09月01日 08:51:54
 */
@RestController
@Tag(name = "环境配置")
@RequestMapping("/ts/config")
public class TsConfigController {

    @Resource
    private ITsConfigService tsConfigService;
    @Resource
    private CommonConfigService commonConfigService;
    @Operation(summary = "新增环境配置",description = "编辑环境配置")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid TsConfigDto dto) {
        boolean b = tsConfigService.insert(dto);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }
    @Operation(summary = "编辑环境配置",description = "编辑环境配置")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid TsConfigDto dto) {
        boolean b = tsConfigService.update(dto);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "删除环境配置",description = "删除环境配置")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = tsConfigService.delete(id);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }



    @Operation(summary = "查看环境配置详情",description = "查看环境配置详情")
    @GetMapping("/get")
    public ResponseResult getConfigById(@RequestParam("id") Long id) {
        return ResponseResult.success(tsConfigService.getConfig(id));
    }

    @Operation(summary = "查看环境配置详情",description = "查看环境配置详情")
    @PostMapping("/list")
    public ResponseResult listConfig(@RequestBody @Valid ConfigListQuery query) {
        return ResponseResult.success(tsConfigService.listConfig(query));
    }

    @Operation(summary = "获取整体环境配置",description = "获取整体环境配置")
    @PostMapping("/getConfigs")
    public ResponseResult getConfigs(@RequestBody @Valid TsConfigReq req) {
        return ResponseResult.success(commonConfigService.getConfigList(req.getConfigQueryList()));
    }

    @Operation(summary = "获取可配置项",description = "获取可配置项")
    @GetMapping("/getConfigurableCode")
    public ResponseResult getConfigurableCode(@RequestParam("type") String type) {
        return ResponseResult.success(tsConfigService.getConfigurableCode(type));
    }

}
